# -*- coding:utf-8 -*-
from database.db_handler import MysqlHander
from common.my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
import json
import socket

'''
获取代理ip
'''

class ProxyDailiyun:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        host_name = socket.gethostname()
        self.host = socket.gethostbyname(host_name)

    def get_proxy_data_url(self):
        return "http://guangtoufangyang.v4.dailiyun.com/query.txt?key=NPX025717A&word=&count=8&rand=false&ltime=100&norepeat=true&detail=true"

    def add_proxy_data(self, ip, port, expire_time):
        insert = "insert into proxy_ip (Fip, Fport, Fhost, Fexpire_time) values "
        insert += "('" + ip + "'," + str(port) + ", '" + self.host + "', '" + expire_time + "');"
        
        print(insert)
        try:
            self.db.execute_not_safe(insert)
        except Exception as e:
            print(str(e))
            pass

    def delete_proxy_data(self):
        delete = "delete from proxy_ip where Fexpire_time < now() and Fhost='" + self.host + "';"
        
        print(delete)
        try:
            self.db.execute_not_safe(delete)
        except Exception as e:
            print(str(e))
            pass
            
    def query_proxy_data(self):
        query = "select count(1) from proxy_ip where Fexpire_time > now() and Fhost='" + self.host + "';"
        
        print(query)
        try:
            data = self.db.query_sql(query)
            if len(data) == 0:
                return 0
            return data[0][0]
        except Exception as e:
            print(str(e))
            pass
        return 10000


    def get_data(self):
        url = self.get_proxy_data_url()
        data = MyHttp.http_json_data(url).decode()
        print(data)
        lines = data.split("\n")
        for line in lines:
            if len(line) < 5:
                continue	
            d = line.split(",")
            p = d[0].split(":")
            ip = p[0]
            port = p[1]
            expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(d[4])))
            self.add_proxy_data(ip, port, expire_time)

    def proxess(self):
        self.delete_proxy_data()
        if self.query_proxy_data() <= 8:
            self.get_data()

if __name__ == '__main__':
    d = ProxyDailiyun()
    d.proxess()
